<?php
/*
 * Created on Feb 5, 2009
 * @author: ANH
 * @ fileName: /modules/Form/Place.php
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
 class node_Form_Node extends Zend_Form
 {
 	 protected $cName = 'node_Form_Node';
     protected $_noDecorator = array(
        'ViewHelper', 
        'Errors'
    );
    
    public function cluBuild($params)
    {
    
        $filterHtmlBody = new Clu_Filter_HtmlBody();
        $filterTrim = new Zend_Filter_StringTrim();
        $filterDigits = new Zend_Filter_Digits();
        $validatorNotEmpty = new Zend_Validate_NotEmpty();
        $myValidator = new Zend_Validate_Regex('/^[A-Z0-9_\s\.\-]*$/i'); //alpha, num, space, -, _
        $myValidator->setMessage(t(60), Zend_Validate_Regex::NOT_MATCH);

        $this->setMethod('POST');
        $actionUrl = $this->getView()->hvnUrl(array('module' => 'node', 
        											'controller' => 'master',
        											'type' => $params['type'],
        											'action' => $params['action']) , 'node');
    	$this->setAction($actionUrl);
    	
    	//Depending on different type of node, maybe get info from db here 
    	//
    	//
    	//
        //node language
	    if ($params['nodeTypeInfo']['needs_translation'])
	    {
	 		$language = new Zend_Form_Element_Select('language',  
	 											  array('disableLoadDefaultDecorators' => true,
										          ));
			$languages = array (t(40), t(41), t(42), t(43));
	        foreach ($languages as $k => $text) {
	            $language->addMultiOption($k, $text);
	        	$language->addDecorators($this->_noDecorator);
	        }
	        
	        $this->addElement($language);
	    }
    	
	    
         //node category
	    if ($params['nodeTypeInfo']['has_category'])
	    {
	    	//$params['nodeTypeInfo'] has 'category_table_name, 'root_category_id'
	    	//require_once (APP_PATH . '/modules/category/Model/Category.php');
	    	//require_once (APP_PATH . '/modules/category/Model/Relations.php');
	    	$mCategory = new category_Model_Category();
			$mCategory->setTables($params['nodeTypeInfo']['category_table_name']);
			// if user_creatable then we should 
			// create a category named $nodeType in table `user_categories` 
			// if it doesn't exist yet
			// otherwise, getTree(0_$userId_$nodeType)
			// something like
			// 0_12_gallery
			// 0_12_mailbox

			// user-created category 
			if (isset($params['nodeTypeInfo']['category_creatable']) && $params['nodeTypeInfo']['category_creatable'])
			{
				//get $rootCategoryId;
				if ($params['nodeTypeInfo']['category_table_name'] != 'user')
					die("Something wrong with the node creation. User is now trying to create a category in ". $params['nodeTypeInfo']['category_table_name'] );
				$ret = $mCategory->getUserCategory($params['nodeTypeInfo']['type']);
				if (!$ret['success'])
					die($ret['errorMessage']);
				else
					$rootCategoryId = $ret['id'];
			}
			else
				$rootCategoryId = $params['nodeTypeInfo']['root_category_id'];

			$categories = $mCategory->getTree($rootCategoryId, 0, 3);
	 		$category = new Zend_Form_Element_Select('category',  
	 											  array('disableLoadDefaultDecorators' => true,
										          ));
													          
			//$category->addMultiOption('0','--'.t(113).'--');
	        foreach ($categories as $c) {
	            $category->addMultiOption($c['id'], p(trim($c['displayName'])));
	        }
	        $this->addElement($category);
        	$category->addDecorators($this->_noDecorator);
	    }

	    
	    //title 
	    if ($params['nodeTypeInfo']['has_title'])
	    {
    		$title = new Zend_Form_Element_Text('title', array('disableLoadDefaultDecorators' => true,
                                                                       'class' => 'inputText', 
                                                                       'required' => true));
            $title->addDecorators($this->_noDecorator)
                      	->addFilter($filterTrim)
            			->addValidator('NotEmpty');
        	$this->addElement($title);
	    }
        
         //story body
	    if ($params['nodeTypeInfo']['has_body'])
	    {
	    	//TODO: $params['nodeTypeInfo']['body_size'] :'small', 'medium',....
	    	// to set differetn sizes for height, width here
	    	//TODO: further configuration can be moved into a text area 
	    	// and configured by a 
    	    $body = new Clu_Form_Element_FckEditor('body', array(
            		'fckOptions' => array (
            						'Height' =>  $params['nodeTypeInfo']['body_fckeditor_height'],
            						'Width'  => $params['nodeTypeInfo']['body_fckeditor_width'],
            						'ToolbarSet' => $params['nodeTypeInfo']['body_fckeditor_toolbar_set']
            					),
            		'required' => true
           			 )
            );
            $body->setLabel(t(742))
            	 ->addValidator($validatorNotEmpty);
            $this->addElement($body);
	    }
        
	    
	    //$keywords 
	    if ($params['nodeTypeInfo']['has_keywords'])
	    {
    		$keywords = new Zend_Form_Element_Text('keywords', array('disableLoadDefaultDecorators' => true,
                                                                       'class' => 'inputText',
                                                                       /*'required' => true */));
            $keywords->addDecorators($this->_noDecorator)
                      	->addFilter($filterTrim)
            			->addValidator('NotEmpty');
        	$this->addElement($keywords);
	    }
        
	    //status
	    if (strpos ($params['userPermissions'], 'set status ' . $params['type']))
        {
            $status = new Zend_Form_Element_Select('status',array('disableLoadDefaultDecorators' => true,
                                                                     'required' => true));
        	/*
            $status	->addMultiOption('featured',t(33))
            	   ->addMultiOption('published',t(34))
                   ->addMultiOption('queued',t(35))
                   ->addMultiOption('unpublished',t(36))
                   ->addMultiOption('deleted',t(37))
			*/
            $status	->addMultiOption('0',t(34))
            	   ->addMultiOption('1',t(33))
                   ->addMultiOption('2',t(35))
                   ->addMultiOption('3',t(36))
                   ->addMultiOption('4',t(37))
                    ->setRegisterInArrayValidator(false)	
                    ->addDecorators($this->_noDecorator);
            $this->addElement($status);
        }

        if ( isset($params['nodeTypeInfo']['requires_captcha']) && $params['nodeTypeInfo']['requires_captcha'] && $params['action'] === 'new' && (strpos ($params['userPermissions'], 'administer '. $params['type']) === false))
		{
            //add check captcha
            $elementCaptcha = 
            		   new Zend_Form_Element_Captcha('captcha_newplace',
                               array('captcha' => 'Image','captchaOptions' =>
                               array(
                                    'captcha' => 'Image',
                                    'wordLen' => 4,
                                    'timeout' => 300,
                                    'imgDir'  =>APP_PATH.'/public/captcha',
                                    'imgUrl' => '/public/captcha/',
                                    'width' => 175,
                                    'height' => 60,
                                    'font' => APP_PATH.'/public/captcha/font/tahoma.ttf',
                                    'DotNoiseLevel' => 10,  
                                    'LineNoiseLevel' => 1,
                                    'expiration' => 3,
                                    'gcFreq' => 1))
            );
            $elementCaptcha->setLabel(t(39).':')
                           ->setAttrib('id','captcha_newplaceImage')
                          ->setRequired(true);
            $this->addElement($elementCaptcha);
		}            
        
        // add the submit button
		if ($params['action'] === 'new')
    		$submit = new Zend_Form_Element_Submit('submit', array('disableLoadDefaultDecorators' => true, 
                                                           'label' => t(58) ));
      	elseif ($params['action'] === 'update')
      		$submit = new Zend_Form_Element_Submit('submit', array('disableLoadDefaultDecorators' => true, 
                                                               'label' => t(59) ));
        
        $submit->addDecorators($this->_noDecorator);
        $this->addElement($submit);        
        

        //node type, to be hidden
		$type = new Zend_Form_Element_Hidden('type', array('disableLoadDefaultDecorators' => true,
															'value' => $params['type']
	                                                                   /*'class' => 'inputText',*/
	                                                                   ));
        $type->addDecorators($this->_noDecorator);
		
        $this->addElement($type);
        
    }
    
    public function populateCategory($data, $categoryId)
    {
    	//Populate rtype options 
    	if ($categoryId != 0)
		{
			$mCategory = new category_Model_Category();
			$mCategory->setTables('place');
		    $categories = $mCategory->getTree($categoryId, 1, 2);
		    function cmp($a, $b)
			{
			    return strcmp($a['name'], $b['name']);
			}
			usort($categories, "cmp");
		    
			foreach ($categories as $c) {
				$c = (array)$c;
				if($c['id'] != 0)
	          		$this->rtype->addMultiOption($c['id'], $c['displayName']);
     		}
		}
        //Populate district options 
    	if (isset($data['city_id']) && $data['city_id'] != 0)
	    {
	       	$mDistrict = new place_Model_District();
	    	$districts = $mDistrict->getDistricts($data['city_id']);
			foreach ($districts as $d) {
          	    $this->district_id->addMultiOption($d['district_id'], $d['district_name']);
     		}
        }
        //populate village options
    	if (isset($data['district_id']) && $data['district_id'] != 0)
	    {
			$mVillage = new place_Model_Village();
		    $villages = $mVillage->getVillages($data['district_id']);
			foreach ($villages as $v) {
          	    $this->village_id->addMultiOption($v['village_id'], $v['village_name']);
     		}
        }
        $data['category'] = $categoryId;
        $this->populate($data);
    }
 }
 
?>
